import Vue from 'vue'
import App from './App'
import './uni.promisify.adaptor'
import qs from 'qs'
import { getUserCardInfo } from '@/request/relationship'

window.qs = qs

Vue.config.productionTip = false
App.mpType = 'app'

import store from './store'
import './utils/prototype'

import EventHub from './utils/bus'
Vue.prototype.$eventHub = EventHub

import objectPrototype from './utils/object-prototype'
objectPrototype()

import vuexCheck from './store/check'
vuexCheck(store)

console.log(JSON.parse(JSON.stringify(store.state.user)));

import interact from './utils/interact'
Vue.prototype.$interact = interact

Vue.config.ignoredElements = [
    'wx-open-launch-weapp',
    /^uni-/
]


const app = new Vue({
    store,
    ...App
})


Vue.mixin({
    data() {
        return {
            mixinIsDevRunTime: process.env.NODE_ENV === 'development' 
        }
    },
    computed: {
        mixinLoginStatus() {
            return !!this.$store.state.user.account.token
        }
    }
})

uni.setStorageSync('openUrl', window.location.href)



async function initPage() {
    const getUserInfo = (token) => {
        return new Promise((resolve, reject) => {
            store.commit('SET_ACCOUNT', { token })
            getUserCardInfo({token}, {
                resolveAllData: true
            }).then(res => {
                if(res.code == 1) {
                    let detail = res.data
                    let info = detail.info
                    delete detail.info
                    store.commit('SET_USER_INFO', {
                        ...detail,
                        ...info, 
                    })
                } else {
                    store.commit('SET_USER_INFO', {})
                }
                resolve()
            })
        })
    }
    let queryStr = location.search.slice(1)
    let query = qs.parse(queryStr)
    if(query.token) {
        await getUserInfo(query.token)
        let searchStr = location.search.slice(1)
        let searchQuery = qs.parse(searchStr)
        delete searchQuery.token
        searchStr = qs.stringify(searchQuery)
        location.href = location.pathname + '?' + searchStr
    }
    app.$mount()
}
initPage()
